package com.hykj.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hykj.dao.NewsDao;
import com.hykj.toolsbean.StringHandler;
import com.hykj.valuebean.news;

public class NewsServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String servletpath = request.getServletPath();
		if ("/news/getAllNews".equals(servletpath)) {
			getAllNews(request, response);
		} else if ("/admin/news/insertNew".equals(servletpath)) {
			insertNew(request, response);
		} else if ("/news/viewNew".equals(servletpath)) {
			viewNew(request, response);
		} else if ("/admin/listNews".equals(servletpath)) {
			listNews(request, response);
		} else if ("/admin/news/viewNew".equals(servletpath)) {
			adminViewNew(request, response);
		} else if ("/admin/news/deleteNew".equals(servletpath)) {
			deleteNew(request, response);
		} else if ("/news/getCategoryNews".equals(servletpath)) {
			getCategoryNews(request, response);
		} else if ("/admin/news/updateNew".equals(servletpath)) {
			updateNew(request, response);
		} else if ("/admin/news/getCategoryNews".equals(servletpath)) {
			adminGetCategoryNews(request, response);
		}
	}
	
	protected void adminGetCategoryNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			NewsDao newsDaoIns = new NewsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String category = request.getParameter("category");
			String goWhich = "/hykj/news/getCategoryNews?category=" + category;
			List NewsList = newsDaoIns.getCategoryNews(strcurrentP, strcurrentG,
					goWhich, category);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", newsDaoIns.getDaoPage());
			newsDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/admin/news/listNews.jsp");
		rd.forward(request, response);
	}

	protected void getAllNews(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			NewsDao newsDaoIns = new NewsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String goWhich = "/hykj/news/getAllNews";
			List NewsList = newsDaoIns.getAllNews(strcurrentP, strcurrentG,
					goWhich);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", newsDaoIns.getDaoPage());
			newsDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/news/news.jsp");
		rd.forward(request, response);
	}
	
	protected void updateNew(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsDao newsDaoIns = new NewsDao();
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
		Date date = new Date();
		String title = request.getParameter("title");
		String author = request.getParameter("author");
		String original = request.getParameter("original");
		String article = request.getParameter("article");
		String category = request.getParameter("category");

		news newIns = new news();
		newIns.setArticle(article);
		newIns.setAuthor(author);
		newIns.setDate(dateformat.format(date));
		newIns.setOriginal(original);
		newIns.setTitle(title);
		newIns.setCategory(category);
		newIns.setId(StringHandler.strToint(request.getParameter("id")));

		newsDaoIns.insertNews(newIns);
		newsDaoIns.closed();

		RequestDispatcher rd = request.getRequestDispatcher("/admin/listNews");
		rd.forward(request, response);
	}

	protected void insertNew(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		NewsDao newsDaoIns = new NewsDao();
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
		Date date = new Date();
		String title = request.getParameter("title");
		String author = request.getParameter("author");
		String original = request.getParameter("original");
		String article = request.getParameter("article");
		String category = request.getParameter("category");

		news newIns = new news();
		newIns.setArticle(article);
		newIns.setAuthor(author);
		newIns.setDate(dateformat.format(date));
		newIns.setOriginal(original);
		newIns.setTitle(title);
		newIns.setCategory(category);

		newsDaoIns.insertNews(newIns);
		newsDaoIns.closed();

		RequestDispatcher rd = request.getRequestDispatcher("/admin/listNews");
		rd.forward(request, response);
	}

	protected void viewNew(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Integer newId = StringHandler.strToint(request.getParameter("newId"));
		if (newId != null) {

			NewsDao newDaoIns = new NewsDao();
			news newsIns = newDaoIns.getSingleNew(newId);
			newDaoIns.closed();

			if (newsIns != null) {
				request.setAttribute("newsIns", newsIns);
			}

		}
		RequestDispatcher rd = request.getRequestDispatcher("/news/viewNew.jsp");
		rd.forward(request, response);
	}

	protected void listNews(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			NewsDao newsDaoIns = new NewsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String goWhich = "/hykj/admin/listNews";
			List<?> NewsList = newsDaoIns.getAllNews(strcurrentP, strcurrentG,
					goWhich);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", newsDaoIns.getDaoPage());
			newsDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/admin/news/listNews.jsp");
		rd.forward(request, response);
	}

	protected void adminViewNew(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Integer newId = StringHandler.strToint(request.getParameter("newId"));
		if (newId != null) {

			NewsDao newDaoIns = new NewsDao();
			news newsIns = newDaoIns.getSingleNew(newId);
			newDaoIns.closed();

			if (newsIns != null) {
				request.setAttribute("newsIns", newsIns);
			}

		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/admin/news/updateNew.jsp");
		rd.forward(request, response);
	}

	protected void deleteNew(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String newId = request.getParameter("newId");
		NewsDao newDaoIns = new NewsDao();
		newDaoIns.deleteNew(newId);
		newDaoIns.closed();
		
		RequestDispatcher rd = request.getRequestDispatcher("/admin/listNews");
		rd.forward(request, response);

	}
	
	protected void getCategoryNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			NewsDao newsDaoIns = new NewsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String category = request.getParameter("category");
			String goWhich = "/hykj/news/getCategoryNews?category=" + category;
			List NewsList = newsDaoIns.getCategoryNews(strcurrentP, strcurrentG,
					goWhich, category);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", newsDaoIns.getDaoPage());
			newsDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/news/news.jsp");
		rd.forward(request, response);
	}

}
